﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Bloodbank.Api;
using Bloodbank.Dto;

namespace Bloodbank
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void tblinkSingup_Click(object sender, EventArgs e)
        {
            
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUsername.Text.Trim() == string.Empty) {
                MessageBox.Show("Username is required", "Message");
            }
            else if (txtPassword.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Password is required", "Message");
            }
            else {
                CheckLoginDto checkedLogin = new CheckLoginDto();
                CheckedLoginApi checkedLoginApi = new CheckedLoginApi();
                checkedLogin = checkedLoginApi.CheckedLogin(txtUsername.Text.Trim(), txtPassword.Text.Trim());
                if (!checkedLogin.IsSuccess)
                {
                    MessageBox.Show(checkedLogin.Message, "Message");
                }
                else {
                    if (checkedLogin.Permission == 1)
                    {
                        frmAdmin frm = new frmAdmin();
                        frm.ShowDialog();
                    }
                    else {
                        frmUserUpdate frm = new frmUserUpdate(checkedLogin.UserId);
                        frm.ShowDialog();
                    }
                }
            }
        }

        private void tblinkSingup_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmRegister frm = new frmRegister();
            frm.ShowDialog();
        }
    }
}
